﻿using System.Collections;
using System.Data;
using System.Linq;

namespace KamLib
{

    #region Entities

    public interface IUilEntitiesSource
    {
        DbFieldID.List KeyFields { get; }
        DbFieldID.List SortFields { get; }
        DbFieldID.List GroupFields { get; }

        bool? CanInsert { get; }
        bool? CanEdit { get; }
        bool? CanDelete { get; }
        bool? IsLocal { get; }
        int? PageSize { get; }

        IEnumerable GetData(QueryArgs e);
    }


    public class UilEntitiesDesc : UilDescription
    {
        public DbFieldID.List Fields { get; set; }
        public DbFieldID.List KeyFields { get; set; }
        public DbFieldID.List GroupFields { get; set; }
        public DbFieldID.List SortFields { get; set; }

        public bool? CanInsertn { get; set; }
        public bool? CanEditn { get; set; }
        public bool? CanDeleten { get; set; }
        public bool? IsLocaln { get; set; }
        public int? PageSizen { get; set; }

        public bool CanInsert { get { return CanInsertn ?? false; } set { CanInsertn = value; } }
        public bool CanEdit { get { return CanEditn ?? false; } set { CanEditn = value; } }
        public bool CanDelete { get { return CanDeleten ?? false; } set { CanDeleten = value; } }
        public bool IsLocal { get { return IsLocaln ?? false; } set { IsLocaln = value; } }
        public int PageSize { get { return PageSizen ?? 0; } set { PageSizen = value; } }


        public UilEntitiesDesc()
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="fields">Имена поля в формате Alias = t.DbName : key, group, asc, desc</param>
        public UilEntitiesDesc(params string[] fields)
        {
            Fields = new DbFieldID.List(fields);
            KeyFields = new DbFieldID.List(Fields.Where(a => a.IsKey));
            GroupFields = new DbFieldID.List(Fields.Where(a => a.IsGroup));
            SortFields = new DbFieldID.List(Fields.Where(a => a.SortDir != QueryArgs.SortDirection.Default));
        }

    }


    public abstract class UilEntitiesController : UilController, IUilEntitiesSource
    {
        protected UilEntitiesDesc EntitiesDesc;
        public abstract IEnumerable GetEntities(QueryArgs e);

        public override void Initialize(UilContext context)
        {
            EntitiesDesc =
                TypeInfo.Descs.OfType<UilEntitiesDesc>().FirstOrDefault()
                ?? new UilEntitiesDesc();

            base.Initialize(context);
        }

        DbFieldID.List IUilEntitiesSource.KeyFields { get { return EntitiesDesc.KeyFields; } }
        DbFieldID.List IUilEntitiesSource.SortFields { get { return EntitiesDesc.SortFields; } }
        DbFieldID.List IUilEntitiesSource.GroupFields { get { return EntitiesDesc.GroupFields; } }
        bool? IUilEntitiesSource.CanInsert { get { return EntitiesDesc.CanInsertn; } }
        bool? IUilEntitiesSource.CanEdit { get { return EntitiesDesc.CanEditn; } }
        bool? IUilEntitiesSource.CanDelete { get { return EntitiesDesc.CanDeleten; } }
        bool? IUilEntitiesSource.IsLocal { get { return EntitiesDesc.IsLocaln; } }
        int? IUilEntitiesSource.PageSize { get { return EntitiesDesc.PageSizen; } }
        IEnumerable IUilEntitiesSource.GetData(QueryArgs e) { return GetEntities(e); }
        
        public IDbConnection cn { get { return Context.DbConnection; } }
    }

    #endregion



    //public interface IUilEntitiesSourceContainer
    //{
    //    IUilEntitiesSource EntitiesSource { get; set; }
    //}


    public interface IUilJScriptSource
    {
        string Script { get; }
    }

    public interface IUilJScriptSourceContainer
    {
        IUilJScriptSource ScriptSource { get; set; }
    }





}